# -*- coding: utf-8 -*
# 游戏数据
#!/usr/bin/python3
import sys
import os
sys.path.append(os.getcwd() + "\\2048")
from Core.Data import GameData
import random

class GameDataHelp:

    # 调用随机数据生成器生成数据
    @staticmethod
    def AddRomdomDataToGameData():
        print("GameController")
        count = GameDataHelp.__GetRomdomCount()
        for i in range(count):
            
            # 获取游戏数据类表
            # 替换数据
            # 设置到游戏中
            add_pos = GameDataHelp.__GetRomdomIndex()
            row = add_pos[0]
            col = add_pos[1]

            add_num = GameDataHelp.__GetRomdomNumer()

            temp_data = GameData.GameData.GetGameData()
            data = list(temp_data)
            data[row][col] = add_num 

            GameData.GameData.SetGameData(data)


    # 调用游戏检测器检测游戏是否还能继续
    @staticmethod
    def CheckHaveEmpySpace():
        if GameDataHelp.__GetRomdomCount() > 0:
            return True
        return False
       


    # 随机填充数字
    @staticmethod
    def __GetRomdomNumer():
        # 五分之二的概率是4
        random_num = random.sample(range(10),1)[0] + 1
        flag = random_num > 4

        if flag:
            return 4
        else:
            return 2
    
    # 随机填充位置
    @staticmethod
    def __GetRomdomIndex():
        #查找出所有可以填充的位置，并且组织成一张列表
        empty_list = GameDataHelp.__GetEmptySpaceList()
        #对可以填充位置的类表进行随机
        index = (random.sample(range(len(empty_list)),1))[0]
        return list(empty_list[index])

    
    # 随机填充次数 最多两次
    @staticmethod
    def __GetRomdomCount():
        # 进行遍历查找出空位置
        empty_list = GameDataHelp.__GetEmptySpaceList()
        count = len(empty_list)
        if count > 2:
            count = 2
        return count

    
    # 获取可以填充位置列表
    @staticmethod
    def __GetEmptySpaceList():
        # 进行遍历查找出空位置
        data = GameData.GameData.GetGameData()
        list = []
        for row in range(len(data)):
            list_info = data[row]
            for col in range(len(list_info)):
                if data[row][col] <= 0:
                    list.append((row,col))
        
        return list


# 测试模块
# for i in iter(range(10)):
#     GameDataHelp.AddRomdomDataToGameData()
#     print(GameDataHelp.CheckHaveEmpySpace())